package com.gopay.common.trace.helper;

import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.trace.TraceLog;
import com.gopay.common.trace.TraceLogUtils;
import com.gopay.common.trace.aop.CpsGenMainOrderDAOAspect;
import com.gopay.common.trace.aop.MainOrderLog;
import com.gopay.common.trace.enums.TraceType;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author ddh02
 * @LogOrderAopHelper
 * @2017/5/26 11:52  www.gopay.com.cn Inc.All rights reserved.
 * 废弃使用  已经改为  使用注解切面形式
 */
@Deprecated
public class LogOrderAopHelper extends LogAbstractHelper {


    @Override
    public void before(Object... args) {



    }

    @Override
    public void after(Object... param) {

        TraceType traceType = null;
        try {
            ProceedingJoinPoint joinPoint = (ProceedingJoinPoint) param[0];
            Object[] args = joinPoint.getArgs();
            MainOrderLog mainOrderLog = new MainOrderLog();

            if(args != null && args.length > 0){
                Object arg = args[0];
                if(arg instanceof CpsGenMainOrder){
                    CpsGenMainOrder cpsGenMainOrder = (CpsGenMainOrder) arg;
                    convert(cpsGenMainOrder,mainOrderLog);
                    String method = joinPoint.getSignature().getName();
                    if("save".equals(method)){
                        traceType = TraceType.ORDER_SAVE; //save
                    } else if("update".equals(method)){
                        traceType = TraceType.ORDER_UPDATE; //update
                    }
                }
            }
            if (traceType != null) {
                TraceLogUtils.log(new TraceLog(traceType, System.currentTimeMillis() - startTime, mainOrderLog));
            }
        } catch (Exception e) {
            printSelfException(e, "order aop after 阶段自身异常！");
        } finally {

        }
    }

    @Override
    public void ex(Throwable throwable) {
        commonProcessEx(throwable);
    }

    /**
     * 多个type  这里不做返回了
     * @return
     */
    @Override
    public TraceType getTraceType() {
        return null;
    }

    private void convert(CpsGenMainOrder cpsGenMainOrder, MainOrderLog mainOrderLog) {
        mainOrderLog.setOrderType(cpsGenMainOrder.getOrderType());
        mainOrderLog.setGopayOrderId(cpsGenMainOrder.getGopayOrderId());
        mainOrderLog.setTransDtlSq(cpsGenMainOrder.getTransDtlSq());
        mainOrderLog.setGopayIntTxnCd(cpsGenMainOrder.getGopayIntTxnCd());
        mainOrderLog.setMerId(cpsGenMainOrder.getMerId());
        mainOrderLog.setMerOrderId(cpsGenMainOrder.getMerOrderId());
        mainOrderLog.setMerTxnTm(cpsGenMainOrder.getMerTxnTm());
        mainOrderLog.setGatewaySource(cpsGenMainOrder.getGatewaySource());
        mainOrderLog.setGatewayChannel(cpsGenMainOrder.getGatewayChannel());
        mainOrderLog.setPayChannel(cpsGenMainOrder.getPayChannel());
        mainOrderLog.setGopayTxnTm(cpsGenMainOrder.getGopayTxnTm());
        mainOrderLog.setTotalOrderAmt(cpsGenMainOrder.getTotalOrderAmt());
        mainOrderLog.setTotalOrderFeeAmt(cpsGenMainOrder.getTotalOrderFeeAmt());
        mainOrderLog.setOrderDealAmt(cpsGenMainOrder.getOrderDealAmt());
        mainOrderLog.setOrderRealFeeAmt(cpsGenMainOrder.getOrderRealFeeAmt());
        mainOrderLog.setMainRecvAcct(cpsGenMainOrder.getMainRecvAcct());
        mainOrderLog.setPayAcct(cpsGenMainOrder.getPayAcct());
        mainOrderLog.setPayCustId(cpsGenMainOrder.getPayCustId());
        mainOrderLog.setTxnStaCd(cpsGenMainOrder.getTxnStaCd());
        mainOrderLog.setOutStlmId(cpsGenMainOrder.getOutStlmId());
        mainOrderLog.setTranFinishTime(cpsGenMainOrder.getTranFinishTime());
        mainOrderLog.setMerTxnAmt(cpsGenMainOrder.getMerTxnAmt());
        mainOrderLog.setAccountPoint(cpsGenMainOrder.getAccountPoint());
        mainOrderLog.setSettleAmt(cpsGenMainOrder.getSettleAmt());
        mainOrderLog.setTid(logContext.getTid());
    }
}
